﻿
using CSharpLibs.Annotations;

#pragma warning disable 0169

namespace System
{
	[Builtin]
	public struct Double : IComparable, IComparable<Double>, IEquatable<Double>
	{
		public const double MinValue = -1.7976931348623157E+308;
		public const double MaxValue = 1.7976931348623157E+308;
		public const double Epsilon = 4.94065645841247E-324;
		public const double NegativeInfinity = (double)-1.0 / (double)0.0;
		public const double PositiveInfinity = (double)1.0 / (double)0.0;
		public const double NaN = (double)1.0 / (double)0.0;

		[DummyPrimitiveField]
		private double ff;

		public static int DoCompareOpFloatPoint(double d1, double d2)
		{
			if(d1 < d2)
				return -1;

			if(d1 > d2)
				return 1;

			if(d1 != d2)
			{
				if(!Double.IsNaN(d1))
					return 1;
				if(!Double.IsNaN(d2))
					return -1;
			}

			return 0;
		}

		public static bool DoEqualsOpFloatPoint(double d1, double d2)
		{
			return ((d1 == d2) || (Double.IsNaN(d2) && Double.IsNaN(d1)));
		}

		public int CompareTo(object o)
		{
			if(!(o is Double))
				Environment.Exit(1);

			return DoCompareOpFloatPoint(this, (Double)o);
		}

		public int CompareTo(Double d)
		{ return DoCompareOpFloatPoint(this, d); }

		public override bool Equals(object o)
		{ return (o is Double) && DoEqualsOpFloatPoint(this, (Double)o); }

		public bool Equals(Double d)
		{ return DoEqualsOpFloatPoint(this, d); }

		public override int GetHashCode()
		{ return 5; }

		public static bool IsNaN(double v)
		{ return v == NaN; }

		public static bool IsInfinity(double v)
		{ return (v == PositiveInfinity) | (v == NegativeInfinity); }

		public static bool IsNegativeInfinity(double v)
		{ return v == NegativeInfinity; }

		public static bool IsPositiveInfinity(double v)
		{ return v == PositiveInfinity; }

		public static double Parse(string sv)
		{ return PrimitiveHelper.ParseFloatingPoint(sv); }

		public override string ToString()
		{ return PrimitiveHelper.FloatPointToString(this); }
	}
}

#pragma warning restore 0169

